#include "HTNSqList.h"

HTNSqList::HTNSqList(int* a, int n)
{
	this->length = 2 * n - 1;
	this->top = n;
	this->data = new HTNode * [2 * n - 1]();
	int temp;
	for (int i = 1; i < n; i++)
	{
		temp = 0;
		while (temp<i)
		{
			if (a[temp]>a[i])
			{
				break;
			}
			temp++;
		}
		for (int j = i; j > temp; j--)
		{
			swap(a[j], a[j - 1]);
		}
	}
	for (int j = 0; j < n; j++)
	{
		this->data[j] = new HTNode(a[j]);
	}
}

HTNSqList::HTNSqList()
{
	this->length = 0;
	this->top = 0;
	this->data = new HTNode * [this->length]();
}

HTNSqList::~HTNSqList()
{
	delete[] data;
}

void HTNSqList::insertElement(HTNode* node)
{
	if (node == nullptr)
	{
		node = new HTNode();
	}
	this->data[top] = node;
	int temp = 0;
	while (temp<this->top)
	{
		if (this->data[temp]->weight>node->weight)
		{
			break;
		}
		temp++;
	}
	for (int i = top; i > temp; i--)
	{
		swap(this->data[i], this->data[i - 1]);
	}
	this->top++;
}

void HTNSqList::insertElement(int nodeWeight)
{
	int temp = 0;
	while (temp < this->top)
	{
		if (this->data[temp]->weight > nodeWeight)
		{
			break;
		}
		temp++;
	}
	for (int i = top; i > temp; i--)
	{
		swap(this->data[i], this->data[i - 1]);
	}
	this->data[temp]->weight = nodeWeight;
	this->top++;
}

void swap(int& a, int& b)
{
	int temp = a;
	a = b;
	b = temp;
}

void swap(HTNode*& a, HTNode*& b)
{
	HTNode* temp = a;
	a = b;
	b = temp;
}
